/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package east2west.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author nguyendat
 */
public class DatabaseUtil {

    private static Logger logger = Logger.getLogger(DatabaseUtil.class.getName());

    /**
     * Get a database connection
     * @return Connection
     */
    public static Connection getConnection() {
        Connection con = null;
        String driver = DatabaseConfig.readKey("sqlDriver");
        String sqlURL = DatabaseConfig.readKey("sqlUrl");
        String sqlUser = DatabaseConfig.readKey("sqlUser");
        String sqlPassword = DatabaseConfig.readKey("sqlPassword");
        try {
            Class.forName(driver);
            con = DriverManager.getConnection(sqlURL, sqlUser, sqlPassword);
            return con;
        } catch (ClassNotFoundException ex) {
            logger.log(Level.SEVERE, null, ex);
            return null;
        } catch (Exception ex) {
            logger.log(Level.SEVERE, null, ex);
            return null;
        }
    }

    public static void closeConnection(Connection con) {
        try {
            if (!con.isClosed()) {
                con.close();
            }
        } catch (SQLException ex) {
            logger.log(Level.SEVERE, null, ex);
        }
    }
}
